﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program21
{
    class Program
    {
        //用方法实现：计算1-100之间的质数和
        //能被1和它本身整出的整数

        public static List<int> list = new List<int>();
        static void Main(string[] args)
        {
            Console.WriteLine(GetPrimeSum(100));
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }

        /// <summary>
        /// 判断是否为质数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static bool IsPrime(int n)
        {
            for (int i = 2; i < n / 2 + 1; i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 求n以内质数和
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static int GetPrimeSum(int n)
        {
            int sum = 0;
            for (int i = 2; i <= n; i++)
            {
                if (IsPrime(i))
                {
                    list.Add(i);
                    sum += i;
                }
            }
            return sum;
        }
    }
}
